package jointpart.assignment3;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import shared.URLList;
import shared.WordList;


/**
 * Searcher traverse the input file and constructs a linked list with word 
 * appearing in the input file. Each file has a list with all the urls where
 * the word appear.
 * 
 * Searcher also searches the linked list  
 */
class Searcher3 {

	/**
	 * findURL searches though a URLList for an url and returns an URLList with
	 * the first element containing the url. If the url is not in the list null
	 * is returned
	 * @param l URLList to search through
	 * @param url String to find
	 * @return URLList if the url is found otherwise null
	 */
	public static URLList findURL (URLList l, String url) {

		//While there are more elements in the list
		while (l != null) {

			//Evaluate if the element contains the url
			if (l.str.equalsIgnoreCase (url)) {

				//Break loop if the url is found
				break;
			}

			//Get the next element in the list
			l = l.nexturl;
		}

		//Return found element or null
		return l;
	}

	/**
	 * findWord searches though a WordList for a word and returns an WordList with
	 * the first element containing the word. If the word is not in the list null
	 * is returned
	 * @param l WordList to search through
	 * @param word String to find
	 * @return
	 */
	public static WordList findWord (WordList l, String word) {

		//While there are more elements in the list
		while (l != null) {

			//Evaluate if the element contains the word
			if (l.str.equalsIgnoreCase(word)) {

				//Break loop if the word is found 
				break;
			}

			//Get the next element in the list
			l = l.next;
		}

		//Return found element or null
		return l;
	}

	/**
	 * readWordList reads through a text file (or String) and creates a WordList 
	 * containing all words in the file. 
	 * @param filename text file to read
	 * @return WordList with all words in the file
	 * @throws IOException
	 */
	public static WordList readWordList (String filename) throws IOException {
		String name = "";
		String currentPage = "";
		WordList wordListHead = null; 
		WordList tempWordList = null;

		// Open the file given as argument
		BufferedReader infile = new BufferedReader(new FileReader(filename));

		//Read the first line
		name = infile.readLine(); 
		// Exit if there is none
		while (name != null) {  
			//If name is a page
			if(name.startsWith("*PAGE:")){	
				//Set the current page
				currentPage = name.substring(6);

				// Read the next line
				name = infile.readLine(); 

				//If the name is not a page
			} else {
				//Search for the name in the wordList
				tempWordList = Searcher3.findWord(wordListHead, name);

				//If the name is in wordList
				if(tempWordList != null){
					//Search for the current page in the urls
					URLList tempURLList = Searcher3.findURL(tempWordList, currentPage);

					//If current page is already there
					if(tempURLList != null){
						//If the word exists and the url is already there read next line
						name = infile.readLine(); 

						//If current page is a new url
					} else {
						//Create new url list with url list tail as url
						tempURLList = new URLList (currentPage, tempWordList.nexturl);
				
						//Append the new head to word list
						tempWordList.nexturl = tempURLList;

						//Read next line
						name = infile.readLine(); 
					}   

					//If name is a new word
				} else {        				
					//Create new leaf
					wordListHead = new WordList (name, new URLList (currentPage, null), wordListHead);
				
					//Read next line
					name = infile.readLine(); 
				}        			
			}          
		}

		//Close file
		infile.close();

		//Return List
		return wordListHead;
	}
}